package com.apanal.qlife.sys.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.apanal.qlife.advice.model.Advice;
import com.apanal.qlife.common.annotation.CurrentUser;
import com.apanal.qlife.common.annotation.LogType;
import com.apanal.qlife.common.annotation.SystemLog;
import com.apanal.qlife.common.constants.Constants;
import com.apanal.qlife.common.page.Page;
import com.apanal.qlife.common.page.PageReturn;
import com.apanal.qlife.common.util.ExcelUtils;
import com.apanal.qlife.common.util.IdWorkerFactory;
import com.apanal.qlife.property.model.Joinus;
import com.apanal.qlife.sys.model.Feedback;
import com.apanal.qlife.sys.model.File;
import com.apanal.qlife.sys.model.User;
import com.apanal.qlife.sys.service.FeedbackService;

/**
 * 意见反馈控制类
 * 
 * 
 * @author shuliangxing
 * 
 * @date 2015-7-21下午5:20:44
 */
@Controller
@RequestMapping("/feedback")
public class FeedbackController {

	@Autowired
	private FeedbackService service;

	/**
	 * 分页查询系统反馈
	 * 
	 * @param feedback
	 * @param page
	 * @param model
	 * @return
	 */
	@SystemLog(module = "反馈管理", operation = "分页查询系统反馈", type = LogType.SELECT)
	@RequiresPermissions("sys:feedbacksys:view")
	@RequestMapping(value = "syslist", method = RequestMethod.GET)
	public String syslist(Feedback feedback, Page page, Model model) {
		PageReturn<Feedback> result = service
				.selectSysfdForPage(feedback, page);
		model.addAttribute(Constants.DEFAULT_RESULT_NAME, result);
		return "sys/feedbacksysList";
	}

	/**
	 * 转向详情页面
	 * 
	 * @param id
	 * @param model
	 * @return
	 */
	@RequiresPermissions("sys:feedbacksys:view")
	@RequestMapping(value = "/{id}/detail", method = RequestMethod.GET)
	public String toDetail(@PathVariable("id") Long id, Model model) {
		model.addAttribute("feedback", service.selectRelationById(id));
		model.addAttribute("op", "查看反馈");
		return "sys/feedbacksysDetail";
	}

	/**
	 * 导出意见反馈<br>
	 * 大数据不采用此方式(即不采用直接写客户端的方式,采用生成临时文件到服务端供下载)
	 * 
	 * @param feedback
	 * @param page
	 * @param type
	 *            类型(excel2003/excel2007)
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("export")
	public String exportExcel(Feedback feedback, String type,
			HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		Page page = new Page();
		// 去除表头行
		page.setPageSize(ExcelUtils.EXCEL_2003_MAX_ROW - 1);
		PageReturn<Feedback> result = service
				.selectSysfdForPage(feedback, page);

		service.exportExcel(result.getEntityList(), type, request, response);
		return null;
	}

	/**
	 * 分页查询我的系统反馈
	 * 
	 * @param feedback
	 * @param page
	 * @param model
	 * @return
	 */
	@SystemLog(module = "反馈管理", operation = "分页查询我的反馈记录", type = LogType.SELECT)
	@RequestMapping(value = "myList", method = RequestMethod.GET)
	public String myList(@CurrentUser User user, Feedback feedback, Page page,
			Model model) {
		feedback.setUseId(user.getUseId());
		PageReturn<Feedback> result = service
				.selectSysfdForPage(feedback, page);
		model.addAttribute(Constants.DEFAULT_RESULT_NAME, result);
		return "personal/feedbackMyList";
	}

	// 跳到添加反馈建议
	@RequestMapping(value = "addFeedback")
	public String addFeedback(Model model, Advice advice, Page page,
			HttpServletRequest request) {
		return "personal/addFeedback";

	}

	// 新增建议与反馈
	@SystemLog(module = "app管理", operation = "新增建议反馈", type = LogType.CREATE)
	@ResponseBody
	@RequestMapping(value = "saveFeedback", method = RequestMethod.POST)
	public String saveFeedback(@CurrentUser User user, Model model,
			Feedback feedBack, String fileJson) {
		feedBack.setFeeId(IdWorkerFactory.nextId());
		feedBack.setUseId(user.getUseId());
		feedBack.setFeeMobile(user.getUseMobilePhone());

		// 将文件信息json字符串转为文件集合对象
		List<File> fileList = JSON.parseArray(fileJson, File.class);
		feedBack.setFileList(fileList);
		return service.insert(feedBack);
	}

	/**
	 * app反馈查询
	 * 
	 * @param model
	 * @return
	 */
	@SystemLog(module = "app管理", operation = "分页查询app意见反馈", type = LogType.SELECT)
	@RequestMapping(value = "appFeedbackList")
	public String appFeedbackList(Model model, Feedback feedback, Page page) {
		PageReturn<Feedback> fd = service.findAppfdList(feedback, page);
		model.addAttribute("result", fd);
		model.addAttribute("stTime", feedback.getStTime());
		model.addAttribute("endTime", feedback.getEndTime());
		return "app/appFeedbackList";
	}

	/**
	 * app合作意向查询
	 * 
	 * @param model
	 * @return
	 */
	@SystemLog(module = "app管理", operation = "分页查询app合作意向", type = LogType.SELECT)
	@RequestMapping(value = "appContractList")
	public String appContractList(Model model, Joinus joinus, Page page) {
		PageReturn<Joinus> joinuspr = service.findAppContractList(joinus, page);
		model.addAttribute("result", joinuspr);
		model.addAttribute("stTime", joinus.getStTime());
		model.addAttribute("endTime", joinus.getEndTime());
		return "app/appContractList";
	}

}
